﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.DirectX.Direct3D;
using System.Drawing;
using Microsoft.DirectX;

namespace EdytorWF.Objects
{
    class Sphere : SimpleObject
    {
        public int Radius { get; set; }
        public int Steps { get; set; }
        private static int counter = 0;

        public Vector3 Position { get; set; }

        public Sphere(Device d, Vector3 pos)
        {
            MyMesh = null;
            Device = d;
            TypeName = "_SPHERE";
            ObjectName = String.Format("Sphere{0}", counter);
            ++counter;

            Steps = 50;
            Radius = 5;

            Position = new Vector3(pos.X, pos.Y, pos.Z);
            ExtendedMaterials = new List<ExtendedMaterial>();
            Textures = new List<Texture>();
            Materials = new List<Material>();
            LoadMesh("");
        }

        public override void ReloadMesh()
        {
        }

        public override void ReloadTexture()
        {
            LoadTexture("..\\..\\Resources\\rock.jpg");
        }

        public override void ReloadObject()
        {
            ReloadMesh();
            ReloadTexture();
        }

        public override void Render()                // Rendering obiektu na device
        {
            if (Device != null)
            {
                Device.VertexFormat = MyMesh.VertexFormat;   //Ustalamy Format Vertexów
                for (int i = 0; i < ExtendedMaterials.Count; i++)      //EM.Length zawiera liczbę submeshów danego mesha
                {
                    Device.Material = Materials[i];            //każdy submesh ma inny materiał
                    Device.SetTexture(0, Textures[i]);         //i inną teksturę
                    MyMesh.DrawSubset(i);                        //rendering i-tego submesha
                }
            }
        }

        public override void LoadTexture(string FileName)
        {
            if (Device == null)
                return;

            TextureFileName = FileName;
            if (Textures != null)
            {
                Textures.Add(TextureLoader.FromFile(Device, TextureFileName));
            }
        }

        public override void LoadMesh(string file_name)
        {
            MeshFileName = file_name;
            Steps = 50;
            Radius = 5;

            List<CustomVertex.PositionNormalTextured> vertices = new List<CustomVertex.PositionNormalTextured>();
            List<int> indices = new List<int>();

            for (int i = 0; i < Steps; i++)
            {
                float teta = ((float)i / Steps) * 2 * (float)Math.PI;

                for (int j = 0; j < Steps; j++)
                {
                    float phi = ((float)j / Steps) * 2 * (float)Math.PI;

                    vertices.Add(new CustomVertex.PositionNormalTextured
                    {
                        X = Position.X + (float)Radius * (float)Math.Sin(teta) * (float)Math.Cos(phi),
                        Y = Position.Y + (float)Radius * (float)Math.Cos(teta),
                        Z = Position.Z + (float)Radius * (float)Math.Sin(teta) * (float)Math.Sin(phi),

                        Tu = (float)j / Steps,
                        Tv = (float)i / Steps
                    });

                    int a = (Steps * i + j) % (Steps * Steps);
                    int b = (Steps * i + j + 1) % (Steps * Steps); // add % Steps 'n' enjoy :P
                    int c = (Steps * (i + 1) + j) % (Steps * Steps);
                    int d = (Steps * (i + 1) + j + 1) % (Steps * Steps); // add % Steps 'n' enjoy :P

                    indices.AddRange(new int[] { b, c, a });
                    indices.AddRange(new int[] { c, b, d });
                }
            }

            MyMesh = new Mesh(indices.Count / 3, vertices.Count, MeshFlags.Managed | MeshFlags.Use32Bit, CustomVertex.PositionNormalTextured.Format, Device);
            MyMesh.IndexBuffer.SetData(indices.ToArray(), 0, LockFlags.None);
            MyMesh.VertexBuffer.SetData(vertices.ToArray(), 0, LockFlags.None);
            MyMesh.ComputeNormals();

            ExtendedMaterials.Add(new ExtendedMaterial());
            Textures.Add(TextureLoader.FromFile(Device, "..\\..\\Resources\\rock.jpg"));
            Materials.Add(new Material
            {
                Ambient = Color.White,
                Diffuse = Color.White
            });

        }
       
    }
}
